package com.rjpacket.supernews.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.rjpacket.supernews.app.databases.DBHelper;
import com.rjpacket.supernews.app.models.CategoryTitle;
import com.rjpacket.supernews.app.tasks.SplashPhotoJsonTask;
import com.rjpacket.supernews.app.tasks.TaskCallOnBack;
import com.rjpacket.supernews.app.utils.DataContainer;
import com.rjpacket.supernews.app.utils.ImageLoader;
import com.rjpacket.supernews.app.utils.MyImageLoadMethod;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;


public class SplashActivity extends FragmentActivity implements TaskCallOnBack {

    private ImageView imageView;
    private Animation animation1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        animation1 = AnimationUtils.loadAnimation(this, R.anim.act_alpha_in);

        Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.act_alpha_out);

        imageView = (ImageView) findViewById(R.id.splash_image);

        ////////////加载数据

        //TODO　加载网络,欢迎页
        String welcomeUrl = "http://appapi.very-news.com//welcome_ad.m?adId=0&version=16299&width=480&height=854&uid=&udid=073CC368CD437982D321EE88ED3EB3AF&version_name=1.6.2&vcode=16299&client=newsapp&terminal=android&cpid=bnf1377_10890_001";

        //加载JSON 数据的
        SplashPhotoJsonTask task = new SplashPhotoJsonTask(this);
        task.execute(welcomeUrl);

        ////////////////

        Animation rightIn = AnimationUtils.loadAnimation(this, R.anim.act_right_move_in);
        Animation leftIn = AnimationUtils.loadAnimation(this, R.anim.act_left_move_in);

        ImageView superNew = (ImageView) findViewById(R.id.splash_super_new);
        ImageView superTitle = (ImageView) findViewById(R.id.splash_super_title);

        superNew.setAnimation(rightIn);
        superTitle.setAnimation(leftIn);

        rightIn.start();
        leftIn.start();

    }

    @Override
    public void callOnBack(Object object) {
        if (object != null && object instanceof JSONObject) {
            JSONObject json = (JSONObject) object;
            try {
                JSONArray img = json.getJSONArray("img");
                int length = img.length();
                for (int i = 0; i < length; i++) {
                    JSONObject imgObject = img.getJSONObject(i);
                    String url = imgObject.getString("url");

                    MyImageLoadMethod.load(url, imageView);

                    imageView.startAnimation(animation1);

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        initDataTitle();

        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //TODO 未完善，如果第一次进入那么加载教程页，否则直接去main
        //用于判断是否显示过教程页
        SharedPreferences sp = getSharedPreferences(Constants.SP_APP, MODE_PRIVATE);
        boolean firstLogin = sp.getBoolean(Constants.SP_KEY_TUTORIAL_SHOW, true);

        if (!firstLogin) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(this, TutorialActivity.class);
            startActivity(intent);
            finish();
        }

    }

    public void initDataTitle() {
        //TODO 加载RadioGroup的被选项与非选项
        DataContainer dc = DataContainer.getInstance();
        List<CategoryTitle> checkedTitles = new LinkedList<CategoryTitle>();
        List<CategoryTitle> unCheckedTitles = new LinkedList<CategoryTitle>();

        DBHelper helper = new DBHelper(this);
        SQLiteDatabase database = helper.getWritableDatabase();

        //选中的情况
        Cursor trueCursor = database.query("titles", null, "checked = ?", new String[]{Integer.toString(1)}, null, null, null);
        if (trueCursor != null) {
            while (trueCursor.moveToNext()) {
                CategoryTitle title = new CategoryTitle();
                title.setId(trueCursor.getInt(trueCursor.getColumnIndex("id")));
                title.setName(trueCursor.getString(trueCursor.getColumnIndex("name")));
                int checked = trueCursor.getInt(trueCursor.getColumnIndex("checked"));
                if (checked == 1) {
                    title.setChecked(true);
                } else {
                    title.setChecked(false);
                }
                title.setType(trueCursor.getString(trueCursor.getColumnIndex("type")));

                checkedTitles.add(title);
            }
            trueCursor.close();
        }

        //未选中的情况
        Cursor falseCursor = database.query("titles", null, "checked = ?", new String[]{Integer.toString(0)}, null, null, null);
        if (falseCursor != null) {
            while (falseCursor.moveToNext()) {
                CategoryTitle title = new CategoryTitle();
                title.setId(falseCursor.getInt(falseCursor.getColumnIndex("id")));
                title.setName(falseCursor.getString(falseCursor.getColumnIndex("name")));
                int checked = falseCursor.getInt(falseCursor.getColumnIndex("checked"));
                if (checked == 1) {
                    title.setChecked(true);
                } else {
                    title.setChecked(false);
                }
                title.setType(falseCursor.getString(falseCursor.getColumnIndex("type")));

                unCheckedTitles.add(title);
            }
            falseCursor.close();

        }

        dc.setCheckedTitles(checkedTitles);
        dc.setUnCheckedTitles(unCheckedTitles);
    }
}
